<template>
    <div class="page-container">
        <Header></Header>

        <!-- 文章详情 -->
        <div class="main-container">
            <div class="content-grid">
                <!-- 左边栏 -->
                <div class="main-content">
                    <div class="article-container">
                        <!-- 面包屑 -->
                        <nav class="breadcrumb" aria-label="Breadcrumb">
                            <ol class="breadcrumb-list">
                                <li class="breadcrumb-item">
                                    <a @click="router.push('/')" class="home-link">首页</a>
                                </li>
                                <li class="breadcrumb-item">
                                    <div class="breadcrumb-separator">
                                        / <span class="breadcrumb-current">正文</span>
                                    </div>
                                </li>
                            </ol>
                        </nav>

                        <!-- 文章主体 -->
                        <article class="article">
                            <h1 class="article-title">{{ article.title }}</h1>
                            <div class="article-meta">
                                <div class="meta-item">
                                    发表于 {{ article.updateTime }}
                                </div>
                                <div class="meta-item">
                                    分类于&nbsp;
                                    <a @click="goCategoryArticleListPage(article.categoryId, article.categoryName)" 
                                       class="category-link">
                                        {{ article.categoryName }}
                                    </a>
                                </div>
                                <div class="meta-item">
                                    阅读量 {{ article.readNum }}
                                </div>
                            </div>

                            <div class="article-content" v-viewer v-html="article.content" v-highlight></div>

                            <!-- 标签 -->
                            <div class="article-tags">
                                <span v-for="(item, index) in article.tags" 
                                      :key="index"
                                      @click="goTagArticleListPage(item.tagId, item.name)" 
                                      class="tag-item">
                                    # {{ item.name }}
                                </span>
                            </div>
                        </article>

                        <!-- 上下篇 -->
                        <div class="article-navigation">
                            <div class="nav-item prev" v-if="article.preArticleId">
                                <a @click="goArticleDetail(article.preArticleId)">
                                    <span class="nav-label">上一篇</span>
                                    <span class="nav-title">{{ article.preArticleTitle }}</span>
                                </a>
                            </div>
                            <div class="nav-item next" v-if="article.nextArticleId">
                                <a @click="goArticleDetail(article.nextArticleId)">
                                    <span class="nav-label">下一篇</span>
                                    <span class="nav-title">{{ article.nextArticleTitle }}</span>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 右边栏 -->
                <div class="sidebar">
                    <div class="sidebar-content">

                        <!-- 文章分类 -->
                        <CategoryCard></CategoryCard>
                        <!-- 文章标签 -->
                        <TagCard></TagCard>
                    </div>
                </div>
            </div>
        </div>

        <Footer />
    </div>
</template>

<script setup>
import Header from '@/layouts/components/Header.vue'
import Footer from '@/layouts/components/Footer.vue'
import { useRoute, useRouter } from 'vue-router';
import { getArticleDetail } from '@/api/frontend/article';
import { ref, reactive } from 'vue'

const router = useRouter()
const route = useRoute()
const article = reactive({
    title: '',
    content: '',
    updateTime: '',
    readNum: 0,
    categoryId: null,
    categoryName: '',
    preArticleId: null,
    preArticleTitle: '',
    nextArticleId: null,
    nextArticleTitle: '',
    tags: [],
})

function queryArticleDetail(articleId) {
    console.log('调用获取详情接口...' + route.query.articleId)
    getArticleDetail(articleId).then((e) => {

        // if (e.success === false) {
        //     ElMessage.warning(e.message)
        //     setTimeout(() => {
        //         router.push('/404')
        //     }, 1500)
        //     return
        // }

        article.title = e.data.title
        article.content = e.data.content
        article.updateTime = e.data.updateTime
        article.categoryId = e.data.categoryId
        article.categoryName = e.data.categoryName
        article.readNum = e.data.readNum
        article.tags = e.data.tags
        if (e.data.preArticle) {
            console.log('上一篇...')
            console.log(e.data.preArticle)
            article.preArticleId = e.data.preArticle.articleId
            article.preArticleTitle = e.data.preArticle.title
        } else {
            article.preArticleId = null
        }

        if (e.data.nextArticle) {
            article.nextArticleId = e.data.nextArticle.articleId
            article.nextArticleTitle = e.data.nextArticle.title
        } else {
            article.nextArticleId = null
        }
    })
}
queryArticleDetail(route.params.articleId);

const goArticleDetail = (articleId) => {
    console.log('跳转详情页' + articleId)
    router.push({ path: `/article/${articleId}` })
    queryArticleDetail(articleId)
}

const goCategoryArticleListPage = (categoryId, name) => {
    router.push({ path: `/category/list/${categoryId}/${name}`})
}

const goTagArticleListPage = (tagId, name) => {
    router.push({ path: `/tag/list/${tagId}/${name}` })
}

</script>

<style scoped>
.page-container {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.main-container {
    flex: 1;
    max-width: 1200px;
    margin: 1.25rem auto;
    padding: 0 15px;
}

.content-grid {
    display: grid;
    grid-template-columns: 3fr 1fr;
    gap: 1rem;
}

.main-content {
    padding: 0 1rem;
}

.article-container {
    background: white;
    border: 1px solid #e5e7eb;
    padding: 1.25rem;
}

/* 面包屑导航样式 */
.breadcrumb {
    margin-bottom: 1rem;
}

.breadcrumb-list {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.breadcrumb-item {
    display: flex;
    align-items: center;
}

.home-link {
    display: flex;
    align-items: center;
    color: #6b7280;
    font-size: 0.875rem;
    cursor: pointer;
}

.home-link:hover {
    color: #2563eb;
}

.breadcrumb-separator {
    display: flex;
    align-items: center;
    color: #9ca3af;
}

.breadcrumb-current {
    margin-left: 0.25rem;
    font-size: 0.875rem;
    color: #6b7280;
}

/* 文章样式 */
.article-title {
    font-size: 2rem;
    font-weight: 700;
    color: #292525;
    line-height: 1.3;
    margin-bottom: 1.25rem;
}

.article-meta {
    display: flex;
    align-items: center;
    gap: 1.25rem;
    margin-bottom: 1.25rem;
    font-size: 0.875rem;
    color: #6b7280;
}

.meta-item {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.category-link {
    color: #6b7280;
    text-decoration: none;
}

.category-link:hover {
    text-decoration: underline;
    color: #2563eb;
}

/* 文章标签 */
.article-tags {
    margin: 1.25rem 0;
}

.tag-item {
    display: inline-block;
    background: #ecfdf5;
    color: #047857;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    margin-right: 0.75rem;
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s;
}

.tag-item:hover {
    background: #d1fae5;
}

/* 文章导航 */
.article-navigation {
    display: flex;
    justify-content: space-between;
    padding-top: 1rem;
    border-top: 1px solid #e5e7eb;
}

.nav-item {
    cursor: pointer;
}

.nav-label {
    display: block;
    font-size: 0.75rem;
    color: #6b7280;
}

.nav-title {
    color: #111827;
}

.nav-title:hover {
    color: #2563eb;
}

/* 侧边栏样式 */
.sidebar {
    padding: 0 1rem;
}

.sidebar-content {
    position: sticky;
    top: 5.25rem;
}

.sidebar-card {
    background: white;
    border: 1px solid #e5e7eb;
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.card-title {
    font-size: 1rem;
    font-weight: 700;
    color: #111827;
    text-transform: uppercase;
    margin-bottom: 1rem;
}

/* 深色模式 */
@media (prefers-color-scheme: dark) {
    .article-container,
    .sidebar-card {
        background: #1f2937;
        border-color: #374151;
    }

    .article-title,
    .card-title {
        color: #f9fafb;
    }

    .category-item {
        color: #e5e7eb;
    }

    .category-item:hover {
        background: #374151;
    }

    .nav-title {
        color: #e5e7eb;
    }
}

/* 响应式布局 */
@media (max-width: 768px) {
    .content-grid {
        grid-template-columns: 1fr;
    }

    .main-content,
    .sidebar {
        padding: 0;
    }
}

/* 调整间距以补偿移除的图标 */
.meta-item {
    gap: 0.25rem;
}

.category-item {
    padding: 0.5rem;
}
</style>